<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;
/**
 * 
 * 账号管理
 */
//class Account extends Adminbase{
  class Account extends controller{
	/**
     * 账号详情页面
     * @return
     */
    public function account_details(){
        $this->assign('verify_type', config('verify_type'));
        return $this->fetch('account_details');
    }
     /**
     * 注册账号页面
     * @return
     */
     public function account_registered(){
        $this->assign('verify_type', config('verify_type'));
        return $this->fetch('account_registered');
    }
     /**
     * 账号操作记录页面
     * @return
     */
    public function account_operation(){
        $this->assign('verify_type', config('verify_type'));
        return $this->fetch('account_operation');
    }
    /**
     * 账号操作记录
     * @return
     */
    public function Operation_record(){
        $username = input("param.username");//用户名
        $account = input("param.account");//账号
        $start_time = input("param.start_time");//开始时间
        $end_time = input("param.end_time");//结束时间
        $operatio_type= input("param.operatio_type");
        $where = [];
        if(!empty($username)){
            $where['username'] = $username;
        }
        if(!empty($account)){
            $where['account'] = $account;
        }
        if($start_time != "" && $end_time !=""){
            $where['time'] = array(array('Egt',$start_time),array('Elt',$end_time), 'and');
        }
        if(!empty($operatio_type)){
            $where['operatio_type'] = $operatio_type;
        }
        $data = [];
        $users = Db::name('account_operation')->where($where)->order("id desc")->select();
        return json($users);
    }
    /**
     * 账号类型
     * @return
     */
    public function account_type(){
    // 	$data = Db::name('auth_group')->field("id,title")->selectOrFail();
    //  return json($data);
     	$data = [];
    	Db::name('auth_group')->field("id,title")->chunk(100,function($users)use(&$data){
             foreach($users as $user){
             	 $data[] = $user;
             }
         });
    	 return json($data);
    }
    /**
     * 账号信息
     * @return
     */
    public function account_information(){
    	$username = input("param.username");//用户名
    	$account = input("param.account");//账号
    	$status = input("param.status");//状态
    	$type = input("param.type");//账号类型
    	$where = [];
    	if(!empty($username)){
    		$where['username'] = $username;
    	}
    	if(!empty($account)){
    		$where['account'] = $account;
    	}
    	if($status != "" ){
    		$where['status'] = $status;
    	}
    	if(!empty($type)){
    		$where['type'] = $type;
    	}
     	$data = [];
     	$users = Db::name('users')->where($where)->select();
     	   foreach($users as $user){
             	$res = Db::name('auth_group')->where("id",$user['type'])->find();
             	$user['type_name'] = $res['title'];
             	$data[] = $user;
             }
    	 return json($data);
    }
    /**
     * 账号禁用
     * @return
     */
    public function account_disable(){
    	$status = input("param.status");//状态
    	$users_id = input("param.guid");//账号id
		$object_name = input("param.object_name");
     	$users = Db::name('users')
     		->where('id',$users_id)
     		->update(['status' => $status]);
     	$operation_data = [
        	'username' => Session::get('username'),
        	'account' => Session::get('account'),
        	'action_object' =>$object_name,
        	'time' =>date("Y-m-d H:i:s",time()),
        	'operatio_type' =>4,

        ];
     	$reslut = Db::name('account_operation')->insert($operation_data);
        if($users){
            return json(['code' => 1, 'url' => '', 'msg' => '禁用成功']);
        }else{
            return json(['code' => 0, 'url' => '', 'msg' => '禁用失败']);
        }
    }
    /**
     * 账号启用
     * @return
     */
    public function account_Enable(){
        $status = input("param.status");//状态
        $users_id = input("param.guid");//账号id
        $object_name = input("param.object_name");
        $users = Db::name('users')
            ->where('id',$users_id)
            ->update(['status' => $status]);
        $operation_data = [
            'username' => Session::get('username'),
            'account' => Session::get('account'),
            'action_object' =>$object_name,
            'time' =>date("Y-m-d H:i:s",time()),
            'operatio_type' =>3,

        ];
        $reslut = Db::name('account_operation')->insert($operation_data);
        if($users){
            return json(['code' => 1, 'url' => '', 'msg' => '启用成功']);
        }else{
            return json(['code' => 0, 'url' => '', 'msg' => '启用失败']);
        }
    }
    /**
     * 信息修改
     * @return
     */
    public function account_modify(){
    	$type = input("param.type");//类型
    	$phone = input("param.phone");//手机号
    	$users_id = input("param.guid");//账号id
    	$username = input("param.username");//用户名
    	$account = input("param.account");//账号
		$object_name =input("param.object_name");
//        $data =array_unique(input("post.data/a"));
//        $game =array_unique(input("post.game/a"));
        $data =input("post.data/a");
        $game =input("post.game/a");

    	$d = [
        		'type' => $type,
     			'phone' => $phone,
     			'username' => $username,
     			'account' =>$account,
        ];
    	if(!empty($data)){
        	$area_id = implode(",",$data);
        	$d['area_id'] = $area_id;
        }
        if(!empty($game)){
            $game_id = implode(",",$game);
            $d['play_id'] = $game_id;
        }
     	$users = Db::name('users')
     		->where('id',$users_id)
     		->update($d);
     	$group = Db::name('auth_group_access')
     		->where('uid',$users_id)
     		->update([

     			'group_id' => $type,

     			]);
     	$operation_data = [
        	'username' => Session::get('username'),
        	'account' => Session::get('account'),
        	'action_object' =>$object_name,
        	'time' =>date("Y-m-d H:i:s",time()),
        	'operatio_type' =>2,//修改

        ];
     	Db::name('account_operation')->insert($operation_data);

	    if($users || $users === 0){
	    	return json(['code' => 1, 'url' => '', 'msg' => '信息更新成功']);
	    }else{
	    	return json(['code' => 0, 'url' => '', 'msg' => '信息更新失败']);
	    }
    }
    /**
     * 信息注册
     * @return
     */
    public function account_register(){
    	$type = input("param.type");//类型
    	$phone = input("param.phone");//手机号
    	$username = input("param.username");//用户名
    	$password = md5(123456);//密码
    	$account = input("param.account");//账号
		$operatio_name = input("param.actionname");
		$object_name =input("param.object_name");
        $data =array_unique(input("post.data/a"));
        $area_id = implode(",",$data);
        $game =array_unique(input("post.game/a"));
        $play_id = implode(",",$game);
		$result = $this->validate(compact('account', 'phone','type','username'), 'RegisterValidate');
        if(true !== $result){
            return json(['code' => -5, 'url' => '', 'msg' => $result]);
        }
    	$users = Db::name('users')->where('account',$account)->find();
        if($users){
            return json(['code' => 0, 'url' => '', 'msg' => '该账户已被注册']);
        }
        $data = [
        	'password' =>$password,
        	'register_time' =>date("Y-m-d H:i:s",time()),
        	'username' => $username,
        	'account' => $account,
        	'type' => $type,
        	'phone' => $phone,
            'area_id'=>$area_id,
            'play_id'=>$play_id

        ];
        $GetId = Db::name('users')->insertGetId($data);

     	$group_data = [
        	'uid' => $GetId,
        	'group_id' => $type,

        ];
     	$group = Db::name('auth_group_access')->insert($group_data);
        $username= Db::name("users")->order('register_time desc')->find();
     	$operation_data = [
        	'username' => Session::get('username'),
        	'account' => Session::get('account'),
        	'action_object' =>$username['username'],
        	'time' =>date("Y-m-d H:i:s",time()),
        	'operatio_type' =>1,//注册

        ];
     	Db::name('account_operation')->insert($operation_data);

	    if($group){
	    	return json(['code' => 1, 'url' => '', 'msg' => '账号添加成功']);
	    }else{
	    	return json(['code' => 0, 'url' => '', 'msg' => '账号添加失败']);
	    }
    }
    /**
     * 账号操作查询
     * @return
     */
    public function account_query_operation(){
    	$starTime = input("param.starTime");//类型
    	$endTime = input("param.endTime");//手机号
    	$username = input("param.username");//用户名
    	$account = input("param.account");//账号
		$operatio_name = input("param.gameType");
		$where = [];
		if(!empty($username)){
    		$where['username'] = $username;
    	}
    	if(!empty($account)){
    		$where['account'] = $account;
    	}
    	if(!empty($operatio_name)){
    		$where['operatio_name'] = $operatio_name;
    	}
	    $reslut = Db::name('account_operation')
	    ->where($where)
	    ->whereTime("time",'between',[$starTime, $endTime])
        ->order("id desc")
	    ->select();
	    return json($reslut);	
    }
    /**
     * 账号所属区域查询
     * @return
     */
    public function Area(){
        $uid = Session::get("uid");
       // $uid = 99;
        $result = Db::name('users')
            ->where("id",$uid)
            ->find();
        $area_id = $result['area_id'];
        $where['aid'] = array('in',$area_id);
        $area =  Db::name('area')
            ->where($where)
            ->select();
//        print_r($area);
        return json($area);
    }
     /**
      * 账号所属区域查询
      * @return
      */
     public function Owngame(){
         $uid = Session::get("uid");
         // $uid = 99;
         $result = Db::name('users')
             ->where("id",$uid)
             ->find();
         $play_id = $result['play_id'];
         $where['id'] = array('in',$play_id);
         $area =  Db::name('play')
             ->where($where)
             ->select();
//        print_r($area);
         return json($area);
     }
     /**
      * 游戏列表
      * @return
      */
     public function Game(){

         $result = Db::name('play')
             ->select();
         return json($result);
     }
     /**
      * 所属游戏
      * @return
      */
     public function Own_game(){
         $id = input("param.id");
//         if(empty($id)){
//             $id = Session::get("uid");
//         }
         $result = Db::name('users')
             ->where("id",$id)
             ->find();
         $area = explode(',',$result['play_id']);
         $arr = [];
         foreach ($area as $k =>$v){
             $name = Db::name("play")
                 ->where("id",$v)
                 ->find();
             array_push($arr,$name['name']);

         }
         return json($arr);
     }

}